home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 August (Alt) / CHIP 2005-08.1.iso / program / code / Firefox_1.0.5.exe / browser.xpi / bin / chrome / browser.jar / content / browser / utilityOverlay.js < prev    next >
Encoding:
JavaScript  |  2005-06-29  |  9.6 KB  |  328 lines

  1.  
  2. /**
  3.  * Communicator Shared Utility Library
  4.  * for shared application glue for the Communicator suite of applications
  5.  **/
  6.  
  7. var goPrefWindow = 0;
  8.  
  9. function getBrowserURL()
  10. {
  11.   return "chrome://browser/content/browser.xul";
  12. }
  13.  
  14. function goToggleToolbar( id, elementID )
  15. {
  16.   var toolbar = document.getElementById(id);
  17.   var element = document.getElementById(elementID);
  18.   if (toolbar)
  19.   {
  20.     var isHidden = toolbar.hidden;
  21.     toolbar.hidden = !isHidden;
  22.     document.persist(id, 'hidden');
  23.     if (element) {
  24.       element.setAttribute("checked", isHidden ? "true" : "false");
  25.       document.persist(elementID, 'checked');
  26.     }
  27.   }
  28. }
  29.  
  30. // urlPref: lets each application have its own throbber URL. example: "messenger.throbber.url"
  31. // event: lets shift+click open it in a new window, etc.
  32. function goClickThrobber( urlPref, e )
  33. {
  34.   var url;
  35.   try {
  36.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  37.                          .getService(Components.interfaces.nsIPrefBranch);
  38.     url = pref.getComplexValue(urlPref, Components.interfaces.nsIPrefLocalizedString).data;
  39.   }
  40.  
  41.   catch(e) {
  42.     url = null;
  43.   }
  44.  
  45.   if ( url )
  46.     openUILink(url, e);
  47. }
  48.  
  49. function getTopWin()
  50. {
  51.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  52.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  53.     var topWindowOfType = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  54.  
  55.     if (topWindowOfType) {
  56.         return topWindowOfType;
  57.     }
  58.     return null;
  59. }
  60.  
  61. function openTopWin( url )
  62. {
  63.     openUILink(url, {})
  64. }
  65.  
  66.  
  67.  
  68. function getBoolPref ( prefname, def )
  69. {
  70.   try { 
  71.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  72.                        .getService(Components.interfaces.nsIPrefBranch);
  73.     return pref.getBoolPref(prefname);
  74.   }
  75.   catch(er) {
  76.     return def;
  77.   }
  78. }
  79.   
  80.  
  81. // openUILink handles clicks on UI elements that cause URLs to load.
  82. function openUILink( url, e, ignoreButton, ignoreAlt )
  83. {
  84.   var where = whereToOpenLink(e, ignoreButton, ignoreAlt);
  85.   openUILinkIn(url, where);
  86. }
  87.  
  88.  
  89. /* whereToOpenLink() looks at an event to decide where to open a link.
  90.  *
  91.  * The event may be a mouse event (click, double-click, middle-click) or keypress event (enter).
  92.  *
  93.  * On Windows, the modifiers are:
  94.  * Ctrl        new tab, selected
  95.  * Shift       new window
  96.  * Ctrl+Shift  new tab, in background
  97.  * Alt         save
  98.  *
  99.  * You can swap Ctrl and Ctrl+shift by toggling the hidden pref
  100.  * browser.tabs.loadBookmarksInBackground (not browser.tabs.loadInBackground, which
  101.  * is for content area links).
  102.  *
  103.  * Middle-clicking is the same as Ctrl+clicking (it opens a new tab) and it is
  104.  * subject to the shift modifier and pref in the same way.
  105.  *
  106.  * Exceptions: 
  107.  * - Alt is ignored for menu items selected using the keyboard so you don't accidentally save stuff.  
  108.  *    (Currently, the Alt isn't sent here at all for menu items, but that will change in bug 126189.)
  109.  * - Alt is hard to use in context menus, because pressing Alt closes the menu.
  110.  * - Alt can't be used on the bookmarks toolbar because Alt is used for "treat this as something draggable".
  111.  * - The button is ignored for the middle-click-paste-URL feature, since it's always a middle-click.
  112.  */
  113. function whereToOpenLink( e, ignoreButton, ignoreAlt )
  114. {
  115.   if (e == null)
  116.     e = { shiftKey:false, ctrlKey:false, metaKey:false, altKey:false, button:0 };
  117.  
  118.   var shift = e.shiftKey;
  119.   var ctrl =  e.ctrlKey;
  120.   var meta =  e.metaKey;
  121.   var alt  =  e.altKey && !ignoreAlt;
  122.  
  123.   // ignoreButton allows "middle-click paste" to use function without always opening in a new window.
  124.   var middle = !ignoreButton && e.button == 1;
  125.   var middleUsesTabs = getBoolPref("browser.tabs.opentabfor.middleclick", true);
  126.  
  127.   // Don't do anything special with right-mouse clicks.  They're probably clicks on context menu items.
  128.  
  129.   if (ctrl || (middle && middleUsesTabs)) {
  130.     if (shift)
  131.       return "tabshifted";
  132.     else
  133.       return "tab";
  134.   }
  135.   else if (alt) {
  136.     return "save";
  137.   }
  138.   else if (shift || (middle && !middleUsesTabs)) {
  139.     return "window";
  140.   }
  141.   else {
  142.     return "current";
  143.   }
  144. }
  145.  
  146. /* openUILinkIn opens a URL in a place specified by the parameter |where|.
  147.  *
  148.  * |where| can be:
  149.  *  "current"     current tab            (if there aren't any browser windows, then in a new window instead)
  150.  *  "tab"         new tab                (if there aren't any browser windows, then in a new window instead)
  151.  *  "tabshifted"  same as "tab" but in background if default is to select new tabs, and vice versa
  152.  *  "window"      new window
  153.  *  "save"        save to disk (with no filename hint!)
  154.  */
  155. function openUILinkIn( url, where )
  156. {
  157.   if (!where)
  158.     return;
  159.  
  160.   if ((url == null) || (url == "")) 
  161.     return;
  162.   // xlate the URL if necessary
  163.   if (url.indexOf("urn:") == 0) {
  164.       url = xlateURL(url);        // does RDF urn expansion
  165.   }
  166.   // avoid loading "", since this loads a directory listing
  167.   if (url == "") {
  168.       url = "about:blank";
  169.   }
  170.  
  171.   if (where == "save") {
  172.     saveURL(url, null, null, true);
  173.     return;
  174.   }
  175.  
  176.   var w = (where == "window") ? null : getTopWin();
  177.   if (!w) {
  178.     openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url);
  179.     return;
  180.   }
  181.   var browser = w.document.getElementById("content");
  182.  
  183.   switch (where) {
  184.   case "current":
  185.     browser.loadURI(url);
  186.     w._content.focus();
  187.     break;
  188.   case "tabshifted":
  189.   case "tab":
  190.     var tab = browser.addTab(url);
  191.  
  192.     // We check the pref here, rather than in whereToOpenLink, because an "open link in tab"
  193.     // context menu item could call openUILinkwhere directly.
  194.     if ((where == "tab") ^ getBoolPref("browser.tabs.loadBookmarksInBackground", false)) {
  195.       browser.selectedTab = tab;
  196.       w._content.focus();
  197.     }
  198.  
  199.     break;
  200.   }
  201. }
  202.  
  203. // Used as an onclick handler for UI elements with link-like behavior.
  204. // e.g. onclick="checkForMiddleClick(this, event);"
  205. function checkForMiddleClick(node, event)
  206. {
  207.   if (event.button == 1) {
  208.     /* Execute the node's oncommand.
  209.      *
  210.      * Using eval() because of bug 246720.  Would like to use node.oncommand(event).
  211.      *
  212.      * Since we're using eval():
  213.      *
  214.      * |event| is correct because the name of this function's formal parameter matches
  215.      * the automatic name of the formal parameter for oncommand, |event|.
  216.      *
  217.      * |this| is incorrect.  To make it correct, we would have to use Function.call.
  218.      */
  219.     eval(node.getAttribute("oncommand"));
  220.  
  221.     // If the middle-click was on part of a menu, close the menu.
  222.     // (Menus close automatically with left-click but not with middle-click.)
  223.     closeMenus(event.target);
  224.   }
  225. }
  226.  
  227. // Closes all popups that are ancestors of the node.
  228. function closeMenus(node)
  229. {
  230.   if ("tagName" in node) {
  231.     if (node.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  232.     && (node.tagName == "menupopup" || node.tagName == "popup"))
  233.       node.hidePopup();
  234.  
  235.     closeMenus(node.parentNode);
  236.   }
  237. }
  238.  
  239. // update menu items that rely on focus
  240. function goUpdateGlobalEditMenuItems()
  241. {
  242.   goUpdateCommand('cmd_undo');
  243.   goUpdateCommand('cmd_redo');
  244.   goUpdateCommand('cmd_cut');
  245.   goUpdateCommand('cmd_copy');
  246.   goUpdateCommand('cmd_paste');
  247.   goUpdateCommand('cmd_selectAll');
  248.   goUpdateCommand('cmd_delete');
  249.   // XXX: implement controller for cmd_SwitchTextDirection
  250.   document.getElementById('cmd_SwitchTextDirection').setAttribute('disabled', !document.commandDispatcher.focusedElement);
  251. }
  252.  
  253. // update menu items that rely on the current selection
  254. function goUpdateSelectEditMenuItems()
  255. {
  256.   goUpdateCommand('cmd_cut');
  257.   goUpdateCommand('cmd_copy');
  258.   goUpdateCommand('cmd_delete');
  259.   goUpdateCommand('cmd_selectAll');
  260. }
  261.  
  262. // update menu items that relate to undo/redo
  263. function goUpdateUndoEditMenuItems()
  264. {
  265.   goUpdateCommand('cmd_undo');
  266.   goUpdateCommand('cmd_redo');
  267. }
  268.  
  269. // update menu items that depend on clipboard contents
  270. function goUpdatePasteMenuItems()
  271. {
  272.   goUpdateCommand('cmd_paste');
  273. }
  274.  
  275. // Gather all descendent text under given document node.
  276. function gatherTextUnder ( root ) 
  277. {
  278.   var text = "";
  279.   var node = root.firstChild;
  280.   var depth = 1;
  281.   while ( node && depth > 0 ) {
  282.     // See if this node is text.
  283.     if ( node.nodeName == "#text" ) {
  284.       // Add this text to our collection.
  285.       text += " " + node.data;
  286.     } else if ( node instanceof HTMLImageElement) {
  287.       // If it has an alt= attribute, use that.
  288.       var altText = node.getAttribute( "alt" );
  289.       if ( altText && altText != "" ) {
  290.         text = altText;
  291.         break;
  292.       }
  293.     }
  294.     // Find next node to test.
  295.     // First, see if this node has children.
  296.     if ( node.hasChildNodes() ) {
  297.       // Go to first child.
  298.       node = node.firstChild;
  299.       depth++;
  300.     } else {
  301.       // No children, try next sibling.
  302.       if ( node.nextSibling ) {
  303.         node = node.nextSibling;
  304.       } else {
  305.         // Last resort is our next oldest uncle/aunt.
  306.         node = node.parentNode.nextSibling;
  307.         depth--;
  308.       }
  309.     }
  310.   }
  311.   // Strip leading whitespace.
  312.   text = text.replace( /^\s+/, "" );
  313.   // Strip trailing whitespace.
  314.   text = text.replace( /\s+$/, "" );
  315.   // Compress remaining whitespace.
  316.   text = text.replace( /\s+/g, " " );
  317.   return text;
  318. }
  319.  
  320. function getShellService()
  321. {
  322.   var shell = null;
  323.   try {
  324.     shell = Components.classes["@mozilla.org/browser/shell-service;1"]
  325.       .getService(Components.interfaces.nsIShellService);
  326.   } catch (e) {}
  327.   return shell;
  328. }
  329.